//
//  JGLeftViewController.m
//  JGShowSidePageDemo
//
//  Created by mac on 15/11/2.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGLeftViewController.h"

#import "JASidePanelController.h"
#import "UIViewController+JASidePanel.h"

#import "JGRightViewController.h"
#import "JGMainViewController.h"

#import "UIButton+Category.h"
#import "UIView+Category.h"
#import "JGSideHelper.h"

@interface JGLeftViewController ()

@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIButton *hideButton;
@property (nonatomic, weak) UIButton *showButton;
@property (nonatomic, weak) UIButton *removeRightPanelButton;
@property (nonatomic, weak) UIButton *addRightPanelButton;
@property (nonatomic, weak) UIButton *changeCenterPanelButton;

@end

@implementation JGLeftViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.titleLabel.center = CGPointMake(floorf(self.sidePanelController.leftVisibleWidth/2.0f), 45.0f);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UILabel *titleLable = [JGSideHelper commonLabelWithFrame:CGRectZero font:[UIFont boldSystemFontOfSize:18.0f] textColor:[UIColor blackColor] lineBreakMode:NSLineBreakByWordWrapping];
    titleLable.backgroundColor = [UIColor clearColor];
    titleLable.text = @"一些功能展示";
    [titleLable sizeToFit];
    titleLable.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self.view addSubview:titleLable];
    _titleLabel = titleLable;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(30.0f, titleLable.bottom + 40, 200.0f, 40.0f);
    button.normalTitle = @"隐藏主视图";
    [button addTarget:self action:@selector(_hideTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    self.hideButton = button;
    
    
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = self.hideButton.frame;
    [button setTitle:@"显示主视图" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(_showTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.hidden = YES;
    [self.view addSubview:button];
    self.showButton = button;
    
    
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = self.hideButton.frame;
    button.top = self.hideButton.bottom + 10;
    [button setTitle:@"移除右视图" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(_removeRightPanelTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    self.removeRightPanelButton = button;
    
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = self.removeRightPanelButton.frame;
    [button setTitle:@"添加右视图" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(_addRightPanelTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.hidden = YES;
    [self.view addSubview:button];
    self.addRightPanelButton = button;
    
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = self.removeRightPanelButton.frame;
    button.top = self.removeRightPanelButton.bottom + 10;
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    [button setTitle:@"回到主视图" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(_changeCenterPanelTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    self.changeCenterPanelButton = button;
    
}

#pragma mark - Button Actions

#pragma mark - 隐藏主视图
- (void)_hideTapped:(id)sender
{
    [self.sidePanelController setCenterPanelHidden:YES animated:YES duration:0.2f];
    self.hideButton.hidden = YES;
    self.showButton.hidden = NO;
}

#pragma mark - 显示主视图
- (void)_showTapped:(id)sender
{
    [self.sidePanelController setCenterPanelHidden:NO animated:YES duration:0.2f];
    self.hideButton.hidden = NO;
    self.showButton.hidden = YES;
}

#pragma mark - 移除右视图
- (void)_removeRightPanelTapped:(id)sender
{
    self.sidePanelController.rightPanel = nil;
    self.removeRightPanelButton.hidden = YES;
    self.addRightPanelButton.hidden = NO;
}

#pragma mark - 添加右视图
- (void)_addRightPanelTapped:(id)sender
{
    self.sidePanelController.rightPanel = [[JGRightViewController alloc] init];
    self.removeRightPanelButton.hidden = NO;
    self.addRightPanelButton.hidden = YES;
}

#pragma mark - 回到主视图
- (void)_changeCenterPanelTapped:(id)sender
{
    self.sidePanelController.centerPanel = [[UINavigationController alloc] initWithRootViewController:[[JGMainViewController alloc] init]];
}
@end
